<?php 
class Room extends CI_Controller
{
	public function index()
	{
		//load model
		$this->load->model('Room_model');
		$this->load->model('Get_DB_Data_model');
		
		$data['allsw'] = $this->Get_DB_Data_model->get_software_all();
		$data['allhw'] = $this->Get_DB_Data_model->get_hardware_all();
		
		$data['roomdata'] = $this->Room_model->get_all_room();
		
		$this->load->view('template/header');
		$this->load->view('room_detail', $data);
		$this->load->view('template/footer');
	}
	
	public function ajax_change_type()
	{
		//load model
		$this->load->model('Room_model');
		
		//get variable
		$type = $this->input->post('ajax_roomtype');
		
		if($type == 'all')
		{
			$data['roomdata'] = $this->Room_model->get_all_room();
		}
		else if($type == 'lab')
		{
			$roomtype = 0;
			$data['roomdata'] = $this->Room_model->get_room_by_type($roomtype);
		}
		else if($type == 'meeting')
		{
			$roomtype = 1;
			$data['roomdata'] = $this->Room_model->get_room_by_type($roomtype);
		}
		
		echo $this->load->view('room_detail_ajax', $data);
	}
	
	//add new room
	public function addroom()
	{
		//load model
		$this->load->model('Room_model');
		
		//get variable
		$roomname = $this->input->post('form_roomname');
		$build = $this->input->post('form_build');
		$roommax = $this->input->post('form_roommax');
		$comcount = $this->input->post('form_comcount');
		
		$software = $this->input->post('form_sw');
		$hardware = $this->input->post('form_hw');
		
		$roomtype = '';
		$roomstat = '';
		
		$sw = "";
		$hw = "";
		
		//get software select data
		for($i=0; $i<count($software); $i++){
			$sw = $sw ."," .$software[$i];
		}
		
		//get hardware select data
		for($i=0; $i<count($hardware); $i++){
			$hw = $hw ."," .$hardware[$i];
		}
		
		$type = $this->input->post('form_roomtype');
		
		if($type == 'lab'){
			$roomtype = '0';
		}
		else if($type == 'meeting'){
			$roomtype = '1';
		}
		
		$status = $this->input->post('form_roomstat');
		
		if($status == 'notready'){
			$roomstat = 0;
		}
		else if($status == 'ready'){
			$roomstat = 1;
		}
		
		$roompic = null;
		
		//send to DB
		$insert = $this->Room_model->addnewroom($roomname, $roomtype, $build, $roompic, $roommax, $comcount, $sw, $hw, $roomstat);
		
		if($insert == true){
			redirect('room/index');
		}
		else{
			echo "Error";
		}
		
	}
	
	//delete room
	public function deleteroom()
	{
		$roomname = $this->uri->segment(3);
		
		//load model
		$this->load->model('Room_model');
		
		$this->Room_model->deleteroom($roomname);
		
		redirect('room/index');
	}
	
	//update room
	public function updateroom()
	{
		
	}
	
}